package uy.edu.um.ltic.commons;

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;

import javax.imageio.ImageIO;
import java.io.FileInputStream;

public class UtilUs {
	 
	
	
	public static boolean veriNom(String nom){
		String oNom = nom + " " ;
		return oNom.matches("([A-Z]*[a-z]*\\s)*");
	} 
	

	      	     
	    public static String getSecurePassword(String password, String salt)
	    {
	        String generatedPassword = null;
	        try {
	        	String defpassword = password + salt;
	            MessageDigest md = MessageDigest.getInstance("MD5");
	            md.update(defpassword.getBytes());
	            
	            byte[] bytes = md.digest(defpassword.getBytes());
	            StringBuilder sb = new StringBuilder();
	            for(int i=0; i< bytes.length ;i++)
	            {
	                sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
	            }
	            generatedPassword = sb.toString();
	        } 
	        catch (NoSuchAlgorithmException e) {
	            e.printStackTrace();
	        }
	        return generatedPassword;
	    }
	     
	    public static String getSalt() throws NoSuchAlgorithmException, NoSuchProviderException
	    {
	        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "SUN");
	        byte[] salt = new byte[16];
	        sr.nextBytes(salt);
	        return salt.toString();
	    }
	
	    
	    
	    
	public static byte[] extractBytesImage(String path) throws IOException {
		// open image
		File imgPath = new File(path);
		BufferedImage bufferedImage = ImageIO.read(imgPath);

		// get DataBufferBytes from Raster
		WritableRaster raster = bufferedImage.getRaster();
		DataBufferByte data = (DataBufferByte) raster.getDataBuffer();

		return (data.getData());
	}
	
	public static byte[] extractBytesFile(String path) throws IOException {
		FileInputStream fileInputStream=null;
		 
        File file = new File(path);
 
        byte[] bFile = new byte[(int) file.length()];
 
        try {
            //convert file into array of bytes
	    fileInputStream = new FileInputStream(file);
	    fileInputStream.read(bFile);
	    fileInputStream.close();
 
	     
        }catch(Exception e){
        	e.printStackTrace();
        }
		return bFile;
	}
}

	 
	
	   

	///\\\\\\\

